package org.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class RegisterFrame extends JFrame {
    private JTextField userIdField;
    private JTextField nameField;
    private JPasswordField passwordField;
    private JPasswordField confirmPasswordField;
    private JComboBox<String> roleCombo;

    public RegisterFrame() {
        setTitle("用户注册");
        setSize(500, 420);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(null);
        getContentPane().setBackground(new Color(245, 245, 245));

        // 标题标签
        JLabel titleLabel = new JLabel("用户注册");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
        titleLabel.setBounds(0, 20, 500, 40);
        add(titleLabel);

        // 用户ID
        JLabel idLabel = new JLabel("用户ID:");
        idLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        idLabel.setBounds(60, 80, 100, 25);
        add(idLabel);

        userIdField = new JTextField();
        userIdField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        userIdField.setBounds(170, 80, 250, 32);
        add(userIdField);

        // 姓名
        JLabel nameLabel = new JLabel("姓名:");
        nameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        nameLabel.setBounds(60, 120, 100, 25);
        add(nameLabel);

        nameField = new JTextField();
        nameField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        nameField.setBounds(170, 120, 250, 32);
        add(nameField);

        // 密码
        JLabel passLabel = new JLabel("密码:");
        passLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        passLabel.setBounds(60, 160, 100, 25);
        add(passLabel);

        passwordField = new JPasswordField();
        passwordField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        passwordField.setBounds(170, 160, 250, 32);
        add(passwordField);

        // 确认密码
        JLabel confirmPassLabel = new JLabel("确认密码:");
        confirmPassLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        confirmPassLabel.setBounds(60, 200, 100, 25);
        add(confirmPassLabel);

        confirmPasswordField = new JPasswordField();
        confirmPasswordField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        confirmPasswordField.setBounds(170, 200, 250, 32);
        add(confirmPasswordField);

        // 角色
        JLabel roleLabel = new JLabel("角色:");
        roleLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        roleLabel.setBounds(60, 240, 100, 25);
        add(roleLabel);

        roleCombo = new JComboBox<>(new String[]{"student", "teacher"});
        roleCombo.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        roleCombo.setBounds(170, 240, 150, 32);
        add(roleCombo);

        // 注册按钮
        JButton registerButton = new JButton("注 册");
        registerButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
        registerButton.setBackground(new Color(70, 130, 180));
        registerButton.setForeground(Color.BLACK);
        registerButton.setFocusPainted(false);
        registerButton.setBounds(190, 290, 120, 40);
        registerButton.addActionListener(this::performRegistration);
        add(registerButton);
    }

    private void performRegistration(ActionEvent e) {
        String userId = userIdField.getText().trim();
        String name = nameField.getText().trim();
        String password = new String(passwordField.getPassword());
        String confirmPassword = new String(confirmPasswordField.getPassword());
        String role = (String) roleCombo.getSelectedItem();

        // 验证输入
        if (userId.isEmpty() || name.isEmpty() || password.isEmpty()) {
            JOptionPane.showMessageDialog(this, "所有字段必须填写", "注册失败", JOptionPane.WARNING_MESSAGE);
            return;
        }

        if (!password.equals(confirmPassword)) {
            JOptionPane.showMessageDialog(this, "两次输入的密码不一致", "注册失败", JOptionPane.WARNING_MESSAGE);
            return;
        }

        // 检查用户ID是否已存在
        if (UserDAO.isUserIdExists(userId)) {
            JOptionPane.showMessageDialog(this, "用户ID已存在", "注册失败", JOptionPane.WARNING_MESSAGE);
            return;
        }

        // 创建新用户
        String passwordHash = PasswordUtil.hashPassword(password);
        User newUser = new User(userId, name, passwordHash, role);

        // 保存到数据库
        if (UserDAO.addUser(newUser)) {
            JOptionPane.showMessageDialog(this, "注册成功！", "注册成功", JOptionPane.INFORMATION_MESSAGE);
            dispose();
        } else {
            JOptionPane.showMessageDialog(this, "注册失败，请重试", "注册失败", JOptionPane.ERROR_MESSAGE);
        }
    }
}